package com.hospital.user.registration.controller;

import com.hospital.user.registration.entity.Ct_user;
import com.hospital.user.registration.entity.DiseaseInfo;
import com.hospital.user.registration.entity.User;
import com.hospital.user.registration.service.LoginServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private LoginServiceI loginServiceI;

    @RequestMapping("/login")
    public String Login() {
        return "/Userr/registration/welcome";
    }

    @RequestMapping("/user")
    public String User() {
        return "/Userr/registration/user";
    }

    @RequestMapping("/intro")
    public String Intro() {
        return "/Wang/hospitalInfo/introduce";
    }

    @RequestMapping("/hello")
    public String Hello() {
        return "/Wang/userInfo/hello";
    }

    @RequestMapping("/hospital")
    public String Hospital() {
        return "/Wang/location/hospitalLocation";
    }

    @RequestMapping("/sub")
    public String Sub() {
        return "/Wang/registration/subscribe";
    }

    @RequestMapping("/userInfo")
    public String UserInfo() {
        return "/Wang/userInfo/userInfo";
    }

    @RequestMapping("/ReLogin")
    public String ReLogin(String id, String pwd, Model model, HttpServletRequest req) {
        User user1 = new User(id, pwd);
        model.addAttribute("id", id);
        model.addAttribute("pwd", pwd);
        //登录
        boolean login = loginServiceI.login(user1);
        if (login) {
            User user = loginServiceI.selectUser(id);
            req.getSession().setAttribute("user", user);
            List<DiseaseInfo> list = loginServiceI.selectDis(id);
            model.addAttribute("list", list);
            List<Ct_user> ct = loginServiceI.selectCt(id);
            model.addAttribute("ct", ct);
            return "/Userr/registration/user";
        } else {
            req.setAttribute("error", "error");
        }
        return "/Userr/registration/welcome";
    }
}
